<!doctype html>
<html>
<head>
  <title>IPFS in the Browser</title>
  <script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
  <script type="text/javascript">
    document.addEventListener('DOMContentLoaded', async () => {
      const node = await Ipfs.create({ repo: 'ipfs-' + Math.random() })
      window.node = node

      const status = node.isOnline() ? 'online' : 'offline'

      console.log(`Node status: ${status}`)
      document.getElementById('status').innerHTML = `Node status: ${status}`

      // You can write more code here to use it. Use methods like
      // node.add, node.get. See the API docs here:
      // https://github.com/ipfs/interface-ipfs-core
    })
  </script>
</head>
<body>
  <h1>IPFS in the Browser</h1>
  <p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <b><em style="background-color:#d7d6d6">node</em></b>. Open the console to play around with it.</p>
  <p>Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.</p>
  <h1 id="status">Node status: offline</h1>

  <h2>Some suggestions</h2>

  <p>Try adding a new file:</p>

  <code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
    const { Buffer } = Ipfs

    async function addFile () {
      const filesAdded = await node.add(Buffer.from('Hello world!'))
      filesAdded.forEach((file) => console.log('successfully stored', file.hash))
    }

    addFile()
  </code>

  <p>You can cat that same file. If you used the exact same string as above ('Hello world!') you should have an hash like this: 'QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY'</p>

  <code style="display:block; white-space:pre-wrap; background-color:#d7d6d6">
    async function catFile () {
      const data = await node.cat('QmQzCQn4puG4qu8PVysxZmscmQ5vT1ZXpqo7f58Uh9QfyY')
      console.log(data.toString())
    }

    catFile()
  </code>
</body>
</html>
